﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MShop.Core ;
using MShop.Core.Extensions ;
using MShop.Core.DataAccess;
using MShop.Core.Domain;
using MShop.Core.Service.Search;
using MShop.Core.Service.Context;

namespace MShop.Web.Areas.Admin.Controllers
{
    public class ArticleController : SecureController
    {
        private readonly ICommonDao<ArticleCategory> _CategoryDao;
        private readonly IArticleDao _ArticleDao;
        private readonly IContentItemService<Article> _SerachService;
        public ArticleController(ICommonDao<ArticleCategory> categoryDao, IArticleDao  articleDao,IContentItemService<Article> _serachService)
        {
            _ArticleDao = articleDao;
            _CategoryDao = categoryDao;
            _SerachService = _serachService;
        }


        public ActionResult Index(int? page,string key)
        {
            var q = _ArticleDao.GetAll();
            if (!string.IsNullOrEmpty(key))
            {
                q = from item in q where item.Title.Contains(key) || item.Content.Contains(key) || item.Summary.Contains(key) select item;
            }
            int pageIndex = 0;
            if (page.HasValue)
            {
                pageIndex = page.Value - 1;
            }
            q= q.OrderByDescending(x=>x.Created);

            MShop.Web.Components.Paging.PagedList<Article> pagedList = new Components.Paging.PagedList<Article>(q, pageIndex, 25);


            return View(pagedList);
        }

        public ActionResult Create()
        {
            var q = new Article();
            ViewData["cid"] = _CategoryDao.GetAll().ConvertListItem("Name", "Id");             
            return View(q);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(Article article)
        {
            if (ModelState.IsValid)
            {
              
                _SerachService.SaveObject(article);
                _SerachService.SaveChanges();                
                return RedirectToAction("index");
            }
            else
            {
                ViewData["cid"] = _CategoryDao.GetAll().ConvertListItem("Name", "Id",article.CId);
                return View(article);
            }
        }


        public ActionResult Edit(int id)
        {
            var q = _ArticleDao.GetById(id);
            ViewData["cid"] = _CategoryDao.GetAll().ConvertListItem("Name", "Id", q.CId);
            return View(q);
        }
         [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(Article article,int id)
        {
           if (ModelState.IsValid)
            {
                Article ac = _ArticleDao.GetById(id);
                UpdateModel<Article>(ac, new string[] { "Source", "Title", "Author", "CId", "IsRedirect", "RedirectUrl", "TitleColor", "Created", "Summary", "Content" });
                _SerachService.SaveChanges();      
                return RedirectToAction("index");
            }
           ViewData["cid"] = _CategoryDao.GetAll().ConvertListItem("Name", "Id", article.CId);
           return View(article);
        }

        public ActionResult Delete(int id)
        {
            var q = _ArticleDao.GetById(id);
            _SerachService.DeleteObject(q);
            _SerachService.SaveChanges();      
            return RedirectToAction("index");
        }

        public ActionResult CategoryIndex()
        {
            return View(_CategoryDao.GetAll().OrderBy(x=>x.Sort));
        }


        public ActionResult CategoryCreate()
        {
            return View(new ArticleCategory());
        }
        [HttpPost]
        public ActionResult CategoryCreate(ArticleCategory c)
        {
            if (ModelState.IsValid)
            {
                _CategoryDao.Save(c);
                _CategoryDao.SaveChanges();
                return RedirectToAction("CategoryIndex");
            }
            return View(c);
          
        }
        public ActionResult CategoryEdit(int id)
        {
            ArticleCategory ac = _CategoryDao.GetById(id);
            return View(ac);
        }

        [HttpPost]
        public ActionResult CategoryEdit(ArticleCategory c,int id)
        {
            if (ModelState.IsValid)
            {
                ArticleCategory ac = _CategoryDao.GetById(id);
                UpdateModel<ArticleCategory>(ac, new string[] { "Name", "Sort" });
                _CategoryDao.SaveChanges();
                return RedirectToAction("CategoryIndex");
            }
            return View(c);

        }

        public ActionResult CategoryDelete(int id)
        {
            ArticleCategory ac = _CategoryDao.GetById(id);
            _CategoryDao.Delete(ac.Article);
            _CategoryDao.Delete(ac);
            _CategoryDao.SaveChanges();
            return RedirectToAction("CategoryIndex");
        }
    }
}
